1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.DrawingArea;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.Widget;
31 private import gtk.c.functions;
32 public  import gtk.c.types;
33 private import std.algorithm;
34 
35 
36 /**
37  * `GtkDrawingArea` is a widget that allows drawing with cairo.
38  * 
39  * ![An example GtkDrawingArea](drawingarea.png)
40  * 
41  * It’s essentially a blank widget; you can draw on it. After
42  * creating a drawing area, the application may want to connect to:
43  * 
44  * - The [signal@Gtk.Widget::realize] signal to take any necessary actions
45  * when the widget is instantiated on a particular display.
46  * (Create GDK resources in response to this signal.)
47  * 
48  * - The [signal@Gtk.DrawingArea::resize] signal to take any necessary
49  * actions when the widget changes size.
50  * 
51  * - Call [method@Gtk.DrawingArea.set_draw_func] to handle redrawing the
52  * contents of the widget.
53  * 
54  * The following code portion demonstrates using a drawing
55  * area to display a circle in the normal widget foreground
56  * color.
57  * 
58  * ## Simple GtkDrawingArea usage
59  * 
60  * ```c
61  * static void
62  * draw_function (GtkDrawingArea *area,
63  * cairo_t        *cr,
64  * int             width,
65  * int             height,
66  * gpointer        data)
67  * {
68  * GdkRGBA color;
69  * GtkStyleContext *context;
70  * 
71  * context = gtk_widget_get_style_context (GTK_WIDGET (area));
72  * 
73  * cairo_arc (cr,
74  * width / 2.0, height / 2.0,
75  * MIN (width, height) / 2.0,
76  * 0, 2 * G_PI);
77  * 
78  * gtk_style_context_get_color (context,
79  * &color);
80  * gdk_cairo_set_source_rgba (cr, &color);
81  * 
82  * cairo_fill (cr);
83  * }
84  * 
85  * int
86  * main (int argc, char **argv)
87  * {
88  * gtk_init ();
89  * 
90  * GtkWidget *area = gtk_drawing_area_new ();
91  * gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100);
92  * gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100);
93  * gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area),
94  * draw_function,
95  * NULL, NULL);
96  * return 0;
97  * }
98  * ```
99  * 
100  * The draw function is normally called when a drawing area first comes
101  * onscreen, or when it’s covered by another window and then uncovered.
102  * You can also force a redraw by adding to the “damage region” of the
103  * drawing area’s window using [method@Gtk.Widget.queue_draw].
104  * This will cause the drawing area to call the draw function again.
105  * 
106  * The available routines for drawing are documented in the
107  * [Cairo documentation](https://www.cairographics.org/manual/); GDK
108  * offers additional API to integrate with Cairo, like [func@Gdk.cairo_set_source_rgba]
109  * or [func@Gdk.cairo_set_source_pixbuf].
110  * 
111  * To receive mouse events on a drawing area, you will need to use
112  * event controllers. To receive keyboard events, you will need to set
113  * the “can-focus” property on the drawing area, and you should probably
114  * draw some user-visible indication that the drawing area is focused.
115  * 
116  * If you need more complex control over your widget, you should consider
117  * creating your own `GtkWidget` subclass.
118  */
119 public class DrawingArea : Widget
120 {
121 	/** the main Gtk struct */
122 	protected GtkDrawingArea* gtkDrawingArea;
123 
124 	/** Get the main Gtk struct */
125 	public GtkDrawingArea* getDrawingAreaStruct(bool transferOwnership = false)
126 	{
127 		if (transferOwnership)
128 			ownedRef = false;
129 		return gtkDrawingArea;
130 	}
131 
132 	/** the main Gtk struct as a void* */
133 	protected override void* getStruct()
134 	{
135 		return cast(void*)gtkDrawingArea;
136 	}
137 
138 	/**
139 	 * Sets our main struct and passes it to the parent class.
140 	 */
141 	public this (GtkDrawingArea* gtkDrawingArea, bool ownedRef = false)
142 	{
143 		this.gtkDrawingArea = gtkDrawingArea;
144 		super(cast(GtkWidget*)gtkDrawingArea, ownedRef);
145 	}
146 
147 
148 	/** */
149 	public static GType getType()
150 	{
151 		return gtk_drawing_area_get_type();
152 	}
153 
154 	/**
155 	 * Creates a new drawing area.
156 	 *
157 	 * Returns: a new `GtkDrawingArea`
158 	 *
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this()
162 	{
163 		auto __p = gtk_drawing_area_new();
164 
165 		if(__p is null)
166 		{
167 			throw new ConstructionException("null returned by new");
168 		}
169 
170 		this(cast(GtkDrawingArea*) __p);
171 	}
172 
173 	/**
174 	 * Retrieves the content height of the `GtkDrawingArea`.
175 	 *
176 	 * Returns: The height requested for content of the drawing area
177 	 */
178 	public int getContentHeight()
179 	{
180 		return gtk_drawing_area_get_content_height(gtkDrawingArea);
181 	}
182 
183 	/**
184 	 * Retrieves the content width of the `GtkDrawingArea`.
185 	 *
186 	 * Returns: The width requested for content of the drawing area
187 	 */
188 	public int getContentWidth()
189 	{
190 		return gtk_drawing_area_get_content_width(gtkDrawingArea);
191 	}
192 
193 	/**
194 	 * Sets the desired height of the contents of the drawing area.
195 	 *
196 	 * Note that because widgets may be allocated larger sizes than they
197 	 * requested, it is possible that the actual height passed to your draw
198 	 * function is larger than the height set here. You can use
199 	 * [method@Gtk.Widget.set_valign] to avoid that.
200 	 *
201 	 * If the height is set to 0 (the default), the drawing area may disappear.
202 	 *
203 	 * Params:
204 	 *     height = the height of contents
205 	 */
206 	public void setContentHeight(int height)
207 	{
208 		gtk_drawing_area_set_content_height(gtkDrawingArea, height);
209 	}
210 
211 	/**
212 	 * Sets the desired width of the contents of the drawing area.
213 	 *
214 	 * Note that because widgets may be allocated larger sizes than they
215 	 * requested, it is possible that the actual width passed to your draw
216 	 * function is larger than the width set here. You can use
217 	 * [method@Gtk.Widget.set_halign] to avoid that.
218 	 *
219 	 * If the width is set to 0 (the default), the drawing area may disappear.
220 	 *
221 	 * Params:
222 	 *     width = the width of contents
223 	 */
224 	public void setContentWidth(int width)
225 	{
226 		gtk_drawing_area_set_content_width(gtkDrawingArea, width);
227 	}
228 
229 	/**
230 	 * Setting a draw function is the main thing you want to do when using
231 	 * a drawing area.
232 	 *
233 	 * The draw function is called whenever GTK needs to draw the contents
234 	 * of the drawing area to the screen.
235 	 *
236 	 * The draw function will be called during the drawing stage of GTK.
237 	 * In the drawing stage it is not allowed to change properties of any
238 	 * GTK widgets or call any functions that would cause any properties
239 	 * to be changed. You should restrict yourself exclusively to drawing
240 	 * your contents in the draw function.
241 	 *
242 	 * If what you are drawing does change, call [method@Gtk.Widget.queue_draw]
243 	 * on the drawing area. This will cause a redraw and will call @draw_func again.
244 	 *
245 	 * Params:
246 	 *     drawFunc = callback that lets you draw
247 	 *         the drawing area's contents
248 	 *     userData = user data passed to @draw_func
249 	 *     destroy = destroy notifier for @user_data
250 	 */
251 	public void setDrawFunc(GtkDrawingAreaDrawFunc drawFunc, void* userData, GDestroyNotify destroy)
252 	{
253 		gtk_drawing_area_set_draw_func(gtkDrawingArea, drawFunc, userData, destroy);
254 	}
255 
256 	/**
257 	 * Emitted once when the widget is realized, and then each time the widget
258 	 * is changed while realized.
259 	 *
260 	 * This is useful in order to keep state up to date with the widget size,
261 	 * like for instance a backing surface.
262 	 *
263 	 * Params:
264 	 *     width = the width of the viewport
265 	 *     height = the height of the viewport
266 	 */
267 	gulong addOnResize(void delegate(int, int, DrawingArea) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
268 	{
269 		return Signals.connect(this, "resize", dlg, connectFlags ^ ConnectFlags.SWAPPED);
270 	}
271 }